﻿package com.ease.flex.remoting
{
	import com.ease.util.RemoteService;
	import com.ease.util.ServiceUtil;
	import com.ease.util.events.MethodReturnEvent;
	import com.ease.framework.CallbackUtil;
	import com.obas.flex.common.event.SessionTimeoutEvent;
	import com.obas.flex.common.util.ClientErrorUtil;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.external.ExternalInterface;

	import mx.managers.BrowserManager;
	import com.obas.access.business.service.BusinessFunctionAccessService;
	import com.obas.access.business.service.BusinessRepositoryAccessService;
	import com.obas.access.business.service.BusinessScenarioAccessService;
	import com.obas.access.business.service.BusinessUsecaseAccessService;
	import com.obas.access.business.service.ParameterPoolAccessService;
	import com.obas.access.business.service.ParameterTypeAccessService;
	import com.obas.access.business.service.SystemFunctionAccessService;
	import com.obas.access.business.service.SystemProjectAccessService;
	import com.obas.access.business.service.SystemScenarioAccessService;
	import com.obas.access.business.service.SystemUsecaseAccessService;
	import com.obas.access.business.service.TestPlanAccessService;
	import com.obas.access.business.service.TestPlanItemAccessService;
	import com.obas.access.business.service.TestTaskAccessService;
	import com.obas.access.business.service.TestTaskItemAccessService;
	import com.obas.access.business.service.TestcaseScriptAccessService;
	import com.obas.business.parameterpool.service.ParameterPoolManagementService;
	import com.obas.business.scriptmanagement.service.ParameterizedScriptManagementService;
	import com.obas.business.taskexecution.service.TaskExecutionManagementService;
	import com.obas.business.testing.service.ExplorerManagementService;

	public class ServiceManagerComObasTestingFlexRemoting {

		private static var _instance:ServiceManagerComObasTestingFlexRemoting = null;

		public function ServiceManagerComObasTestingFlexRemoting(singleton:SingletonClass) {
			var contextPath:String = ExternalInterface.call("BrowserHistory.getURL");
			RemoteService.defaultChannelUri = ServiceUtil.getDefaultChannelUri(contextPath);
		}

		public static function onOperationReturn(eventDispatcher:EventDispatcher, event:MethodReturnEvent):void {
	    	//var response:Event = null;
			//response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
			//eventDispatcher.dispatchEvent(response);
			
			var response:Event = null;
         	//Session timeout
         	if( null != event.error && ClientErrorUtil.getErrorType(event.error) == ClientErrorUtil.AUTHENTICATION_ERROR) {
            	var sessionTimeoutEvent:SessionTimeoutEvent = new SessionTimeoutEvent(SessionTimeoutEvent.SESSION_TIMEOUT);
            	eventDispatcher.dispatchEvent(sessionTimeoutEvent);
            	response = CallbackUtil.createResponseFromRequest(event.callContext, null, null);
         	} else {
            	response = CallbackUtil.createResponseFromRequest(event.callContext, event.result, event.error);
         	}
         	eventDispatcher.dispatchEvent(response);
		}

	    public static function getInstance():ServiceManagerComObasTestingFlexRemoting {
	        if (_instance == null) {
				_instance = new ServiceManagerComObasTestingFlexRemoting(new SingletonClass());
	        }

	        return _instance;
	    }
		private var _businessFunctionAccessService:BusinessFunctionAccessService = null;

		public static function getBusinessFunctionAccessService():BusinessFunctionAccessService {
			if (getInstance()._businessFunctionAccessService == null) {
				getInstance()._businessFunctionAccessService = new BusinessFunctionAccessService();
			}
			return getInstance()._businessFunctionAccessService;
		}

		public function set enabledBusinessFunctionAccessService(value:Boolean):void {
			if (getInstance()._businessFunctionAccessService) {
				getInstance()._businessFunctionAccessService.enabled = value;
			}
		}
		private var _businessRepositoryAccessService:BusinessRepositoryAccessService = null;

		public static function getBusinessRepositoryAccessService():BusinessRepositoryAccessService {
			if (getInstance()._businessRepositoryAccessService == null) {
				getInstance()._businessRepositoryAccessService = new BusinessRepositoryAccessService();
			}
			return getInstance()._businessRepositoryAccessService;
		}

		public function set enabledBusinessRepositoryAccessService(value:Boolean):void {
			if (getInstance()._businessRepositoryAccessService) {
				getInstance()._businessRepositoryAccessService.enabled = value;
			}
		}
		private var _businessScenarioAccessService:BusinessScenarioAccessService = null;

		public static function getBusinessScenarioAccessService():BusinessScenarioAccessService {
			if (getInstance()._businessScenarioAccessService == null) {
				getInstance()._businessScenarioAccessService = new BusinessScenarioAccessService();
			}
			return getInstance()._businessScenarioAccessService;
		}

		public function set enabledBusinessScenarioAccessService(value:Boolean):void {
			if (getInstance()._businessScenarioAccessService) {
				getInstance()._businessScenarioAccessService.enabled = value;
			}
		}
		private var _businessUsecaseAccessService:BusinessUsecaseAccessService = null;

		public static function getBusinessUsecaseAccessService():BusinessUsecaseAccessService {
			if (getInstance()._businessUsecaseAccessService == null) {
				getInstance()._businessUsecaseAccessService = new BusinessUsecaseAccessService();
			}
			return getInstance()._businessUsecaseAccessService;
		}

		public function set enabledBusinessUsecaseAccessService(value:Boolean):void {
			if (getInstance()._businessUsecaseAccessService) {
				getInstance()._businessUsecaseAccessService.enabled = value;
			}
		}
		private var _parameterPoolAccessService:ParameterPoolAccessService = null;

		public static function getParameterPoolAccessService():ParameterPoolAccessService {
			if (getInstance()._parameterPoolAccessService == null) {
				getInstance()._parameterPoolAccessService = new ParameterPoolAccessService();
			}
			return getInstance()._parameterPoolAccessService;
		}

		public function set enabledParameterPoolAccessService(value:Boolean):void {
			if (getInstance()._parameterPoolAccessService) {
				getInstance()._parameterPoolAccessService.enabled = value;
			}
		}
		private var _parameterTypeAccessService:ParameterTypeAccessService = null;

		public static function getParameterTypeAccessService():ParameterTypeAccessService {
			if (getInstance()._parameterTypeAccessService == null) {
				getInstance()._parameterTypeAccessService = new ParameterTypeAccessService();
			}
			return getInstance()._parameterTypeAccessService;
		}

		public function set enabledParameterTypeAccessService(value:Boolean):void {
			if (getInstance()._parameterTypeAccessService) {
				getInstance()._parameterTypeAccessService.enabled = value;
			}
		}
		private var _systemFunctionAccessService:SystemFunctionAccessService = null;

		public static function getSystemFunctionAccessService():SystemFunctionAccessService {
			if (getInstance()._systemFunctionAccessService == null) {
				getInstance()._systemFunctionAccessService = new SystemFunctionAccessService();
			}
			return getInstance()._systemFunctionAccessService;
		}

		public function set enabledSystemFunctionAccessService(value:Boolean):void {
			if (getInstance()._systemFunctionAccessService) {
				getInstance()._systemFunctionAccessService.enabled = value;
			}
		}
		private var _systemProjectAccessService:SystemProjectAccessService = null;

		public static function getSystemProjectAccessService():SystemProjectAccessService {
			if (getInstance()._systemProjectAccessService == null) {
				getInstance()._systemProjectAccessService = new SystemProjectAccessService();
			}
			return getInstance()._systemProjectAccessService;
		}

		public function set enabledSystemProjectAccessService(value:Boolean):void {
			if (getInstance()._systemProjectAccessService) {
				getInstance()._systemProjectAccessService.enabled = value;
			}
		}
		private var _systemScenarioAccessService:SystemScenarioAccessService = null;

		public static function getSystemScenarioAccessService():SystemScenarioAccessService {
			if (getInstance()._systemScenarioAccessService == null) {
				getInstance()._systemScenarioAccessService = new SystemScenarioAccessService();
			}
			return getInstance()._systemScenarioAccessService;
		}

		public function set enabledSystemScenarioAccessService(value:Boolean):void {
			if (getInstance()._systemScenarioAccessService) {
				getInstance()._systemScenarioAccessService.enabled = value;
			}
		}
		private var _systemUsecaseAccessService:SystemUsecaseAccessService = null;

		public static function getSystemUsecaseAccessService():SystemUsecaseAccessService {
			if (getInstance()._systemUsecaseAccessService == null) {
				getInstance()._systemUsecaseAccessService = new SystemUsecaseAccessService();
			}
			return getInstance()._systemUsecaseAccessService;
		}

		public function set enabledSystemUsecaseAccessService(value:Boolean):void {
			if (getInstance()._systemUsecaseAccessService) {
				getInstance()._systemUsecaseAccessService.enabled = value;
			}
		}
		private var _testPlanAccessService:TestPlanAccessService = null;

		public static function getTestPlanAccessService():TestPlanAccessService {
			if (getInstance()._testPlanAccessService == null) {
				getInstance()._testPlanAccessService = new TestPlanAccessService();
			}
			return getInstance()._testPlanAccessService;
		}

		public function set enabledTestPlanAccessService(value:Boolean):void {
			if (getInstance()._testPlanAccessService) {
				getInstance()._testPlanAccessService.enabled = value;
			}
		}
		private var _testPlanItemAccessService:TestPlanItemAccessService = null;

		public static function getTestPlanItemAccessService():TestPlanItemAccessService {
			if (getInstance()._testPlanItemAccessService == null) {
				getInstance()._testPlanItemAccessService = new TestPlanItemAccessService();
			}
			return getInstance()._testPlanItemAccessService;
		}

		public function set enabledTestPlanItemAccessService(value:Boolean):void {
			if (getInstance()._testPlanItemAccessService) {
				getInstance()._testPlanItemAccessService.enabled = value;
			}
		}
		private var _testTaskAccessService:TestTaskAccessService = null;

		public static function getTestTaskAccessService():TestTaskAccessService {
			if (getInstance()._testTaskAccessService == null) {
				getInstance()._testTaskAccessService = new TestTaskAccessService();
			}
			return getInstance()._testTaskAccessService;
		}

		public function set enabledTestTaskAccessService(value:Boolean):void {
			if (getInstance()._testTaskAccessService) {
				getInstance()._testTaskAccessService.enabled = value;
			}
		}
		private var _testTaskItemAccessService:TestTaskItemAccessService = null;

		public static function getTestTaskItemAccessService():TestTaskItemAccessService {
			if (getInstance()._testTaskItemAccessService == null) {
				getInstance()._testTaskItemAccessService = new TestTaskItemAccessService();
			}
			return getInstance()._testTaskItemAccessService;
		}

		public function set enabledTestTaskItemAccessService(value:Boolean):void {
			if (getInstance()._testTaskItemAccessService) {
				getInstance()._testTaskItemAccessService.enabled = value;
			}
		}
		private var _testcaseScriptAccessService:TestcaseScriptAccessService = null;

		public static function getTestcaseScriptAccessService():TestcaseScriptAccessService {
			if (getInstance()._testcaseScriptAccessService == null) {
				getInstance()._testcaseScriptAccessService = new TestcaseScriptAccessService();
			}
			return getInstance()._testcaseScriptAccessService;
		}

		public function set enabledTestcaseScriptAccessService(value:Boolean):void {
			if (getInstance()._testcaseScriptAccessService) {
				getInstance()._testcaseScriptAccessService.enabled = value;
			}
		}
		private var _parameterPoolManagementService:ParameterPoolManagementService = null;

		public static function getParameterPoolManagementService():ParameterPoolManagementService {
			if (getInstance()._parameterPoolManagementService == null) {
				getInstance()._parameterPoolManagementService = new ParameterPoolManagementService();
			}
			return getInstance()._parameterPoolManagementService;
		}

		public function set enabledParameterPoolManagementService(value:Boolean):void {
			if (getInstance()._parameterPoolManagementService) {
				getInstance()._parameterPoolManagementService.enabled = value;
			}
		}
		private var _parameterizedScriptManagementService:ParameterizedScriptManagementService = null;

		public static function getParameterizedScriptManagementService():ParameterizedScriptManagementService {
			if (getInstance()._parameterizedScriptManagementService == null) {
				getInstance()._parameterizedScriptManagementService = new ParameterizedScriptManagementService();
			}
			return getInstance()._parameterizedScriptManagementService;
		}

		public function set enabledParameterizedScriptManagementService(value:Boolean):void {
			if (getInstance()._parameterizedScriptManagementService) {
				getInstance()._parameterizedScriptManagementService.enabled = value;
			}
		}
		private var _taskExecutionManagementService:TaskExecutionManagementService = null;

		public static function getTaskExecutionManagementService():TaskExecutionManagementService {
			if (getInstance()._taskExecutionManagementService == null) {
				getInstance()._taskExecutionManagementService = new TaskExecutionManagementService();
			}
			return getInstance()._taskExecutionManagementService;
		}

		public function set enabledTaskExecutionManagementService(value:Boolean):void {
			if (getInstance()._taskExecutionManagementService) {
				getInstance()._taskExecutionManagementService.enabled = value;
			}
		}
		private var _explorerManagementService:ExplorerManagementService = null;

		public static function getExplorerManagementService():ExplorerManagementService {
			if (getInstance()._explorerManagementService == null) {
				getInstance()._explorerManagementService = new ExplorerManagementService();
			}
			return getInstance()._explorerManagementService;
		}

		public function set enabledExplorerManagementService(value:Boolean):void {
			if (getInstance()._explorerManagementService) {
				getInstance()._explorerManagementService.enabled = value;
			}
		}

	}
}
class SingletonClass{}
